package com.svms.service.dml.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.svms.domain.dao.SvmsTransportMapper;
import com.svms.model.SvmsTransport;
import com.svms.service.dml.iface.TransportDMLIF;
import com.svms.service.validators.TransportValidator;
import com.svms.service.vo.ResultCodeVO;
import com.svms.service.vo.TransportVO;

public class TransportDML implements TransportDMLIF {

	private static Logger logger = Logger.getLogger(TransportDML.class);

	private SvmsTransport svmsTransport;

	@Autowired
	private SvmsTransportMapper svmsTransportMapper;

	public ResultCodeVO resultSet;

	public TransportValidator transportValidator;

	public TransportValidator getTransportValidator() {
		return transportValidator;
	}

	public void setTransportValidator(TransportValidator transportValidator) {
		this.transportValidator = transportValidator;
	}

	@Override
	public ResultCodeVO insertNewTransport(TransportVO transport) {
		logger.debug("inside insertNewTransport  method");
		try {
			if (transportValidator.checkMandatory(transport)) {
				if (svmsTransport == null) {
					svmsTransport = new SvmsTransport();
				}
				resultSet = new ResultCodeVO();

				svmsTransport.setCreateTime(new Date());
				svmsTransport.setUpdateBy("ad");
				svmsTransport.setUpdatedTime(new Date());
				svmsTransport.setCompanyName(transport.getCompanyName());
				svmsTransport.setAddressLine1(transport.getTransportAddress().getAddressLine1());
				svmsTransport.setAddressLine2(transport.getTransportAddress().getAddressLine2());
				svmsTransport.setEmail(transport.getTransportAddress().getEmail());
				svmsTransport.setPhoneNo(transport.getTransportAddress().getPhone());
				svmsTransportMapper.insert(svmsTransport);

				
			} else {
				
			}
			logger.debug("exiting from insertNewTransport method");
		} catch (Exception e) {
			logger.error("Error occured in insertNewTransport method");
			
			return resultSet;
		}
		return resultSet;
	}

	@Override
	public ResultCodeVO deleteTransport(TransportVO transport) {
		logger.debug("inside deleteTransport method");
		try {
			if (svmsTransport == null) {
				svmsTransport = new SvmsTransport();
			}
			resultSet = new ResultCodeVO();
			System.out.println("===> "+svmsTransportMapper.deleteByPrimaryKey(Long.parseLong(transport
					.getTransportId())));
			logger.debug("exiting from deleteTransport method");
		} catch (Exception e) {
			logger.error("Error occured in deleteTransport method");
			return resultSet;
		}
		return resultSet;
	}

	@Override
	public SvmsTransport getTransport(TransportVO transport) {
		logger.debug("inside getTransport method");
		if (svmsTransport == null) {
			svmsTransport = new SvmsTransport();
		}
		try {
			svmsTransport = svmsTransportMapper.selectByPrimaryKey(Long
					.parseLong(transport.getTransportId()));
			logger.debug("exiting from getTransport method");

		} catch (Exception e) {
			logger.error("Error occured in getTransport method");
			e.printStackTrace();
			return svmsTransport;
		}
		return svmsTransport;
	}

	@Override
	public List<SvmsTransport> getTransportList() {
		logger.debug("inside getTransportList method");
		List<SvmsTransport> resultSet = null;
		try {

			resultSet = svmsTransportMapper.getEntireTableData();

			logger.debug("exiting from getTransportList method");

		} catch (Exception e) {
			logger.error("Error occured in getTransportList method");
			e.printStackTrace();
			return resultSet;
		}
		return resultSet;
	}
}
